library(tidyverse)
library(tictoc)
library(data.table)
library(vroom)
library(fs)
library(glue)

runs <- c("full", "nocontradict")
for (x in runs) {
    if (x == "full") {
        path1 <- "data/output/fit-strata/cd-level/{yr}"
        path2 <- "data/output/fit-strata/state-level/{yr}"
        path3 <- "data/output/fit-chunk-tidy/{yr}/chunk-{s}.csv.gz"
        path4 <- "data/output/fit-strata/cd-level/{yr}/strata-{s}.csv.gz"
        path5 <- "data/output/fit-strata/state-level/{yr}/strata-{s}.csv.gz"
    } else if (x == "nocontradict") {
        path1 <- "data/output/fit-strata_noc/cd-level/{yr}"
        path2 <- "data/output/fit-strata_noc/state-level/{yr}"
        path3 <- "data/output/fit-chunk-tidy_noc/{yr}/chunk-{s}.csv.gz"
        path4 <- "data/output/fit-strata_noc/cd-level/{yr}/strata-{s}.csv.gz"
        path5 <- "data/output/fit-strata_noc/state-level/{yr}/strata-{s}.csv.gz"
    }
iter_grp_vars <- c("cd", "race", "iter")

# about 5 min, 2021-07-18
for (yr in c(2016, 2020)) {
  dir_create(glue(path1))
  dir_create(glue(path2))

  tic()
  for (s in state.abb) {

    chunk_s <- glue(path3)
    # if (!file_exists(chunk_s)) next

    out_s <- vroom(chunk_s, col_types = "cciidd", delim = ",")
    strat_est <- data.table(out_s)[,
                                   list(p_mrp_ggfix = sum(pred_ggfix)/sum(N),
                                        p_mrp_nofix = sum(pred_nofix)/sum(N),
                                        N = sum(N)),
                                   by = iter_grp_vars]
    st_est <- data.table(out_s)[,
                                list(p_mrp_ggfix = sum(pred_ggfix)/sum(N),
                                     p_mrp_nofix = sum(pred_nofix)/sum(N),
                                     N = sum(N)),
                                by = c("race", "iter")]
    rm(out_s)

    fwrite(strat_est, glue(path4))
    fwrite(st_est, glue(path5))
  }
  toc()
}
}
